home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / BoxMaker++ / ll-R ƒ / ll_r_shell.cp next >
Encoding:
Text File  |  1995-06-14  |  7.7 KB  |  348 lines  |  [TEXT/KAHL]

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <QuickDraw.h>
  4. #include <OSUtils.h>
  5. #include <ToolUtils.h>
  6. #include <Menus.h>
  7. #include <Packages.h>
  8. #include <Traps.h>
  9. #include <Files.h>
  10. #include <Aliases.h>
  11. #include <AppleEvents.h>
  12. #include <GestaltEqu.h>
  13. #include <Processes.h>
  14. #include <Fonts.h>
  15. #include <OSEvents.h>
  16. #include <Resources.h>
  17. #include <Desk.h>
  18.  
  19. #include "standardgetfile.h"
  20. #include "boxmakergetfile.h"
  21.  
  22. #include "boxmaker constants.h"
  23. #include "boxmaker.h"
  24. #include "preferences.cp"
  25. #include "ll-R constants.h"
  26. #include "ll_r_shell.h"
  27. #include "outfile.h"
  28.  
  29. #pragma template_access public
  30.  
  31. void main()
  32. {
  33.     StringHandle prefsFileHandle = GetString( 128);
  34.  
  35.     ll_r_settings defaultSettings =
  36.     {
  37.         false,        // item 0 is not used in dialogs
  38.         true,        // kType
  39.         true,        // kCreator
  40.         true,        // kSize
  41.         false,        // kBytes
  42.         true,        // kKiloBytes
  43.         false,        // kCreatDate
  44.         true,        // kModDate
  45.         false,        // kBakDate
  46.         false,        // kTimes
  47.  
  48.         true,        // kTabs
  49.         false,        // kSpaces
  50.         false,        // kAskFileName
  51.         true        // kIndentFolders
  52.     };
  53.     ll_r_shell it( *(Str255 *)*prefsFileHandle, defaultSettings);
  54.     it.run();
  55. }
  56.  
  57. ll_r_shell::ll_r_shell( Str255 prefsFilename, ll_r_settings defaultsettings)
  58.     : boxmaker( 3000)
  59.     , ll_r_prefs( prefsFilename, defaultsettings)
  60. {
  61.     indentationLevel = 0;
  62.     outFileNo = 0;
  63.     SetupDialog( gMainDialog);
  64. }
  65.  
  66. void ll_r_shell::OpenDoc( Boolean opening)
  67. {
  68.     StartNewLine();
  69.     if( !itsADirectory())
  70.     {
  71.         if( myPrefs[ kCreator])
  72.         {
  73.             append( (char *)&theCInfoPBRec.hFileInfo.ioFlFndrInfo.fdCreator, 4);
  74.         }
  75.         if( myPrefs[ kType])
  76.         {
  77.             append( (char *)&theCInfoPBRec.hFileInfo.ioFlFndrInfo.fdType, 4);
  78.         }
  79.         if( myPrefs[ kSize])
  80.         {
  81.             Str255 sizeString;
  82.             long totalsize = theCInfoPBRec.hFileInfo.ioFlLgLen + theCInfoPBRec.hFileInfo.ioFlRLgLen;
  83.             
  84.             if( myPrefs[ kKiloBytes])
  85.             {
  86.                 totalsize = KiloBytes( totalsize);
  87.             }
  88.             NumToString( totalsize, sizeString);
  89.             if( myPrefs[ kSpaces])
  90.             {
  91.                 const int spacesBefore = 5 - sizeString[ 0];
  92.                 for( int i = 0; i < spacesBefore; i++)
  93.                 {
  94.                     append( ' ');
  95.                 }
  96.             }
  97.             append( sizeString);
  98.  
  99.             if( myPrefs[ kKiloBytes])
  100.             {
  101.                 RemoveLastChar();        // the delimiter added by 'append( sizeString)'
  102.                 append( 'K');
  103.                 append( delimiter);
  104.             }
  105.         }
  106.         if( myPrefs[ kCreatDate])
  107.         {
  108.             appendDate( theCInfoPBRec.hFileInfo.ioFlCrDat);
  109.         }
  110.         if( myPrefs[ kModDate])
  111.         {
  112.             appendDate( theCInfoPBRec.hFileInfo.ioFlMdDat);
  113.         }
  114.         if( myPrefs[ kBakDate])
  115.         {
  116.             appendDate( theCInfoPBRec.hFileInfo.ioFlBkDat);
  117.         }
  118.     }
  119.     //
  120.     // append filename:
  121.     //
  122.     append( theCInfoPBRec.hFileInfo.ioNamePtr);
  123.     ChangeLastToNewLine();
  124.  
  125.     long numtowrite = currentEnd - plentySpace;
  126.  
  127.     (void)FSWrite( outFileNo, &numtowrite, (Ptr)plentySpace);
  128. }
  129.  
  130. void ll_r_shell::CantEnterFolder( Boolean opening)
  131. {
  132.     StartNewLine();
  133.     //
  134.     // 'OpenDoc' wrote a line with the name of the folder on it and ended
  135.     // it with a newline. We want to add an error message to that line.
  136.     // Therefore we overwrite that newline by the first character of the
  137.     // error message (which starts with a tab).
  138.     //
  139.     StringHandle message = GetString( 131);
  140.  
  141.     append( *message);
  142.  
  143.     ChangeLastToNewLine();
  144.  
  145.     long numtowrite = currentEnd - plentySpace;
  146.     (void)SetFPos( outFileNo, fsFromLEOF, -1);
  147.     (void)FSWrite( outFileNo, &numtowrite, (Ptr)plentySpace);
  148. }
  149.  
  150. ll_r_shell::~ll_r_shell()
  151. {
  152.     if( outFileNo != 0)
  153.     {
  154.         (void)FSClose( outFileNo);
  155.         (void)FlushVol( 0, outVRefNum);
  156.     }
  157. }
  158.  
  159. void ll_r_shell::StartABunch( long numTopLevelItems, Boolean opening)
  160. {
  161.     boxmaker::StartABunch( numTopLevelItems, opening);
  162.     if( outFileNo == 0)
  163.     {
  164.         StringHandle outName = GetString( 129);
  165.         if( myPrefs[ kAskFileName])
  166.         {
  167.             StandardFileReply stdReply;
  168.             StringHandle prompt = GetString( 130);
  169.             StandardPutFile( *prompt, *outName, &stdReply);
  170.             ReleaseResource( (Handle)prompt);
  171.             if( stdReply.sfGood)
  172.             {
  173.                 OSErr result = noErr;
  174.                 if( stdReply.sfReplacing)
  175.                 {
  176.                     result = FSpDelete( &stdReply.sfFile);
  177.                 }
  178.                 if( result == noErr)
  179.                 {
  180.                     result = FSpCreate( &stdReply.sfFile,
  181.                                 'R*ch', 'TEXT', stdReply.sfScript);
  182.                     if( result == noErr)
  183.                     {
  184.                         result = FSpOpenDF( &stdReply.sfFile, fsWrPerm, &outFileNo);
  185.                         outVRefNum = stdReply.sfFile.vRefNum;
  186.                     }
  187.                 }
  188.                 if( result != noErr)
  189.                 {
  190.                     ErrorAlertQuit( result, kFileCreationFailed);
  191.                 }
  192.             } else {
  193.                 ErrorAlertQuit( noErr, kNeedAFile);
  194.             }
  195.         } else {
  196.             Handle folderTypeHandle = Get1Resource( 'Wher', 128);
  197.             const OSType folderType = **((OSType **)folderTypeHandle);
  198.             ReleaseResource( folderTypeHandle);
  199.             outfile desktopFile( *(Str255 *)*outName, folderType);
  200.             outFileNo  = desktopFile();
  201.             outVRefNum = desktopFile.vRefNum();
  202.         }
  203.         ReleaseResource( (Handle)outName);
  204.     }
  205. }
  206.  
  207. void ll_r_shell::SetupDialog( DialogPtr theDialog)
  208. {
  209.     short    iType;
  210.     Handle    iHandle;
  211.     Rect    iRect;
  212.  
  213.     for( int i = 1; i < kNumButtons; i++)
  214.     {
  215.         GetDialogItem( theDialog, i, &iType, &iHandle, &iRect);
  216.         SetControlValue( (ControlHandle)iHandle, myPrefs[ i]);
  217.     }
  218.     delimiter = "\t "[ myPrefs[ kSpaces]];
  219.  
  220.     const int enable_SizeCheckBoxes = myPrefs[ kSize];
  221.  
  222.     GetDialogItem( theDialog, kBytes, &iType, &iHandle, &iRect);
  223.     HiliteControl( (ControlHandle) iHandle, enable_SizeCheckBoxes ? 0 : 255);
  224.     
  225.     GetDialogItem( theDialog, kKiloBytes, &iType, &iHandle, &iRect);
  226.     HiliteControl( (ControlHandle) iHandle, enable_SizeCheckBoxes ? 0 : 255);
  227.  
  228.     const int enable_kTimes = myPrefs[ kCreatDate]
  229.                         || myPrefs[ kModDate] || myPrefs[ kBakDate];
  230.  
  231.     GetDialogItem( theDialog, kTimes, &iType, &iHandle, &iRect);
  232.     HiliteControl( (ControlHandle) iHandle, enable_kTimes ? 0 : 255);
  233. }
  234.  
  235. void ll_r_shell::HandleDialogEvent( short itemHit, DialogPtr theDialog)
  236. {
  237.     switch( itemHit)
  238.     {
  239.         case kType:
  240.         case kCreator:
  241.         case kTimes:
  242.         case kAskFileName:
  243.         case kIndentFolders:
  244.             FlipButton( theDialog, itemHit);
  245.             break;
  246.  
  247.         case kSize:
  248.             {
  249.                 short    iType;
  250.                 Handle    iHandle;
  251.                 Rect    iRect;
  252.                 FlipButton( theDialog, itemHit);
  253.                 const int enable_SizeCheckBoxes = myPrefs[ kSize];
  254.             
  255.                 GetDialogItem( theDialog, kBytes, &iType, &iHandle, &iRect);
  256.                 HiliteControl( (ControlHandle) iHandle, enable_SizeCheckBoxes ? 0 : 255);
  257.                 
  258.                 GetDialogItem( theDialog, kKiloBytes, &iType, &iHandle, &iRect);
  259.                 HiliteControl( (ControlHandle) iHandle, enable_SizeCheckBoxes ? 0 : 255);
  260.             }
  261.             break;
  262.  
  263.         case kBytes:
  264.         case kKiloBytes:
  265.             if( !myPrefs[ itemHit])
  266.             {
  267.                 short    iType;
  268.                 Handle    iHandle;
  269.                 Rect    iRect;
  270.     
  271.                 FlipButton( theDialog, kBytes);
  272.                 FlipButton( theDialog, kKiloBytes);
  273.             }
  274.             break;            
  275.  
  276.         case kCreatDate:
  277.         case kModDate:
  278.         case kBakDate:
  279.             {
  280.                 short    iType;
  281.                 Handle    iHandle;
  282.                 Rect    iRect;
  283.     
  284.                 FlipButton( theDialog, itemHit);
  285.     
  286.                 const int enable_kTimes = myPrefs[ kCreatDate]
  287.                         || myPrefs[ kModDate] || myPrefs[ kBakDate];
  288.     
  289.                 GetDialogItem( theDialog, kTimes, &iType, &iHandle, &iRect);
  290.                 HiliteControl( (ControlHandle) iHandle, enable_kTimes ? 0 : 255);
  291.             }
  292.             break;            
  293.  
  294.         case kTabs:
  295.         case kSpaces:
  296.             if( !myPrefs[ itemHit])
  297.             {
  298.                 FlipButton( theDialog, kTabs);
  299.                 FlipButton( theDialog, kSpaces);
  300.                 
  301.                 delimiter = "\t "[ itemHit == kSpaces];
  302.             }
  303.             break;
  304.     }
  305. }
  306.  
  307. void ll_r_shell::appendDate( unsigned long seconds)
  308. {
  309.     DateTimeRec theDate;
  310.     SecondsToDate( seconds, &theDate);
  311.     
  312.     append2Digits( theDate.year);
  313.     append2Digits( theDate.month);
  314.     append2Digits( theDate.day);
  315.     append( delimiter);
  316.  
  317.     if( myPrefs[ kTimes])
  318.     {
  319.         append2Digits( theDate.hour);
  320.         append( ':');
  321.         append2Digits( theDate.minute);
  322.         append( ':');
  323.         append2Digits( theDate.second);
  324.         append( delimiter);
  325.     }
  326. }
  327.  
  328. void ll_r_shell::FlipButton( DialogPtr theDialog, short theItem)
  329. {
  330.     short    iType;
  331.     Handle    iHandle;
  332.     Rect    iRect;
  333.  
  334.     myPrefs[ theItem] = !myPrefs[ theItem];
  335.  
  336.     GetDialogItem( theDialog, theItem, &iType, &iHandle, &iRect);
  337.     SetControlValue( (ControlHandle)iHandle, myPrefs[ theItem]);
  338. }
  339.  
  340. void ll_r_shell::append( char *item, int len)
  341. {
  342.     for( int i = 0; i < len; i++)
  343.     {
  344.         *currentEnd++ = *item++;
  345.     }
  346.     append( delimiter);
  347. }
  348.